﻿#ifndef DATASIMUTHREAD_H
#define DATASIMUTHREAD_H

#include <QThread>
#include <QSettings>

#include "datastruct.h"
#include "videocontroller.h"
#include "config/jsonconfig.h"
#include "simufactory/simufactory.h"

class DataSimuThread : public QThread
{
    Q_OBJECT
public:
    explicit DataSimuThread(QSettings* settings, JsonConfig* jsonConfig, QObject *parent = nullptr);
    ~DataSimuThread() override;

    // 停止线程
    void stop();
    // 设置通道数据
    void setChannelDescriptionList(const QList<ChannelDescription>& channelDescriptionList);
    // 设置具体的流程测试步骤
    void setStepList(const QString& subflowName, const QList<Step>& stepList);
    // 设置步骤等待/继续
    void setStepWait(const bool& wait);
    // 复位功能模拟
    void assignReset(bool resetted);

protected:
    virtual void run() override;

private:
    void init();
    void simuStep(const Step& step);
    void handleFault(const Step& step);
    void waiting();
    void simulating(const qint64& timeStamp);


private:
    // INI配置文件
    QSettings* m_settings;

    // JSON配置文件
    JsonConfig* m_jsonConfig;

    // 线程运行标识
    bool m_running;

    // 仿真时间间隔: ms
    int m_simuInterval;

    // 作为比较的时间, 两次时间之差判断时间间隔
    qint64 m_prevTimeStamp;

    // 作为基准进行比较的时间
    qint64 m_baseTimeStamp;
    // 是否需要等待
    bool m_wait;

    // 数据通道列表
    QList<ChannelDescription> m_channelDescriptionList;
    // 数据仿真工厂
    SimuFactory m_simuFactory;

    // 流程测试名称
    QString m_subflowName;
    // 流程测试步骤
    QList<Step> m_stepList;

    // 视频播放控制
    VideoController* m_videoController;


signals:
    void sigTimeReady();
    void sigFaultOccurred(const QString faultCode);
    void sigStepWait(const QString stepName);
    void sigSendCmd(const QString stepName);
    void sigVideoPlay(const int startTime, const int duration);
    void sigSubflowFinished(const QString subflowName);
    void sigCompleteFinished();
    void sigStepNotify(const QString stepCode);
    void sigStorage(const qint64 timeStamp, const QVariantList valueList);


};

#endif // DATASIMUTHREAD_H
